package com.jrew.gae.stacktracer;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.jrew.gae.stacktracer.constant.AppConstants;
import com.jrew.gae.stacktracer.entities.Record;
import com.jrew.gae.stacktracer.services.RecordManagerService;
import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;

/**
 * Process all user request
 * Save new records and fetch old
 *
 */
public class AddRecordServlet extends HttpServlet {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -8023391599968504711L;
	private static final String UTF_ENCODING = "UTF-8";
	
	private RecordManagerService recordManagerService;
	
	/**
	 * 
	 */
	public AddRecordServlet() {
		recordManagerService = new RecordManagerService();
	}
	
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		processRequest(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
	
	/**
	 * @param request
	 * @param response
	 */
	private void processRequest(HttpServletRequest request, HttpServletResponse response){
		try{
			String addRecordFlag = request.getParameter(AppConstants.REQUEST_ADD_RECORD);
			if(addRecordFlag != null){
				addNewRecord(request, response);
				return;
			}
			// There is no any keys in request. Go to the add record page
			forwardToAddRecordPage(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			forwardToAddRecordPage(request, response);
		}
	}
	
	/**
	 * @param request
	 * @param response
	 */
	private void forwardToAddRecordPage(HttpServletRequest request, HttpServletResponse response){
		try{
			RequestDispatcher requestDispatcher = request.getRequestDispatcher(AppConstants.ADD_RECORD_JSP_PATH);
			requestDispatcher.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @param request
	 * @param response
	 */
	private void addNewRecord(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		request.setCharacterEncoding(UTF_ENCODING);
		String newRecordContent = request.getParameter(AppConstants.TEXT_AREA_NAME);
		Record record = recordManagerService.addNewRecordService(newRecordContent);
		request.setAttribute(AppConstants.RECORD_ID, record.getId());
		request.setAttribute(AppConstants.RECORD_PASSWORD, record.getPassword());
		try{
			RequestDispatcher requestDispatcher =  request.getRequestDispatcher('/' + AppConstants.REQUEST_GET_RECORD);
			requestDispatcher.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @param request
	 * @return
	 */
	private boolean checkCaptcha(HttpServletRequest request) {
		String remoteAddr = request.getRemoteAddr();
        ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
        reCaptcha.setPrivateKey(AppConstants.PRIVATE_CAPTCHA_KEY);
        String challenge = request.getParameter("recaptcha_challenge_field");
        String uresponse = request.getParameter("recaptcha_response_field");
        ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);
        if (reCaptchaResponse.isValid()) {
          return true;
        }
        return false;
	}
	
}
